package com.cosmoplat.xikai.business.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cosmoplat.xikai.business.domain.FaultConfiguration;
import com.cosmoplat.xikai.business.domain.FaultTemplate;
import com.cosmoplat.xikai.business.domain.dto.scada.FaultConfigurationDto;
import com.cosmoplat.xikai.business.domain.dto.scada.FaultInformationDto;
import com.cosmoplat.xikai.business.service.FaultConfigurationService;
import com.cosmoplat.xikai.business.service.FaultInformationService;
import com.cosmoplat.xikai.common.core.domain.AjaxResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class FaultInformationServiceImpl implements FaultInformationService {
    @Autowired
    private FaultConfigurationService faultConfigurationService;


    @Override
    public AjaxResult batchSave(MultipartFile multipartFile) throws IOException {

        Path tempPath = Files.createTempFile("UPLOAD", UUID.randomUUID().toString());
        if (multipartFile != null && !multipartFile.isEmpty()) {
            multipartFile.transferTo(tempPath);
        } else {
            return AjaxResult.error("批量导入文件为空！");
        }

        File file = tempPath.toFile();
        EasyExcel.read(file, FaultTemplate.class, new AnalysisEventListener<FaultTemplate>() {

            @Override
            public void invoke(FaultTemplate faultTemplate, AnalysisContext analysisContext) {
                FaultConfiguration faultConfiguration1 = faultConfigurationService.getOne(new LambdaQueryWrapper<FaultConfiguration>().eq(FaultConfiguration::getFaultCode, faultTemplate.getFaultCode()));
                if (faultConfiguration1 != null) {
                    faultConfiguration1.setFaultCode(faultTemplate.getFaultCode());
                    faultConfiguration1.setFaultName(faultTemplate.getFaultName());
                    faultConfiguration1.setFaultDescription(faultTemplate.getFaultDescription());
                    faultConfiguration1.setNotes(faultTemplate.getNotes());
                    faultConfiguration1.setFaultIdentification(faultTemplate.getFaultIdentification().equals("是") ? 1 : 0);
                    faultConfiguration1.setFaultUpdateTime(new Date());
                    faultConfigurationService.updateById(faultConfiguration1);
                } else {
                    FaultConfiguration faultConfiguration = new FaultConfiguration();
                    BeanUtils.copyProperties(faultTemplate, faultConfiguration);
                    faultConfiguration.setFaultIdentification(faultTemplate.getFaultIdentification().equals("是") ? 1 : 0);
                    faultConfiguration.setFaultCreateTime(new Date());
                    faultConfigurationService.save(faultConfiguration);
                }

            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {

            }
        }).sheet().headRowNumber(2).doRead();
        return AjaxResult.success("批量导入成功！");
    }

    @Override
    public AjaxResult removeByIds(Long[] ids) {
        try {
            List<Long> list = Arrays.stream(ids).collect(Collectors.toList());
            faultConfigurationService.removeByIds(list);
            return AjaxResult.success("删除成功");
        } catch (Exception e) {
            return AjaxResult.error("删除失败");
        }
    }

    @Override
    public AjaxResult getById(int id) {
        try {
            FaultConfiguration faultConfiguration = faultConfigurationService.getById(id);
            if (faultConfiguration == null) {
                return AjaxResult.error("查询失败");
            }
            return AjaxResult.success(faultConfiguration);
        } catch (Exception e) {
            return AjaxResult.error("删除失败");
        }
    }

    @Override
    public AjaxResult save(FaultInformationDto faultInformationDto) {
        FaultConfiguration faultConfiguration = new FaultConfiguration();
        if (faultInformationDto.getId() != null) {
            faultConfiguration = faultConfigurationService.getById(faultInformationDto.getId());
        }
        faultConfiguration.setFaultDescription(faultInformationDto.getFaultDescription());
        faultConfiguration.setFaultName(faultInformationDto.getFaultName());
        faultConfiguration.setFaultCode(faultInformationDto.getFaultCode());
        faultConfiguration.setFaultIdentification(faultInformationDto.getFaultIdentification());
        faultConfiguration.setNotes(faultInformationDto.getNotes());
        if (faultInformationDto.getId() != null) {
            faultConfiguration.setFaultUpdateTime(new Date());
            faultConfigurationService.updateById(faultConfiguration);
            return AjaxResult.success("修改成功");
        } else {
            FaultConfiguration faultConfiguration1 = faultConfigurationService.getOne(new LambdaQueryWrapper<FaultConfiguration>().eq(FaultConfiguration::getFaultCode, faultInformationDto.getFaultCode()));
            if (faultConfiguration1 != null) return AjaxResult.error("故障代码重复");
            faultConfiguration.setFaultCreateTime(new Date());
            faultConfiguration.setFaultIdentification(faultInformationDto.getFaultIdentification());
            faultConfigurationService.save(faultConfiguration);
            return AjaxResult.success("添加成功");
        }
    }

    @Override
    public List<FaultConfiguration> selectConfigList(FaultConfigurationDto faultConfigurationDto) {
        return faultConfigurationService.selectList(faultConfigurationDto);
    }
}
